package com.atguigu.controller;

        import com.atguigu.base.BaseController;
        import com.atguigu.entity.*;
        import com.atguigu.service.*;
        import com.github.pagehelper.PageInfo;
        import com.sun.org.apache.xpath.internal.operations.Mod;
        import org.apache.dubbo.config.annotation.DubboReference;
        import org.apache.dubbo.config.annotation.DubboService;
        import org.springframework.stereotype.Controller;
        import org.springframework.ui.ModelMap;
        import org.springframework.web.bind.annotation.GetMapping;
        import org.springframework.web.bind.annotation.PathVariable;
        import org.springframework.web.bind.annotation.RequestMapping;

        import javax.servlet.http.HttpServletRequest;
        import java.util.List;
        import java.util.Map;

@Controller
@RequestMapping("/house")
public class HouseController extends BaseController {

   @DubboReference
    private HouseService houseService;
    @DubboReference
    private DictService dictService;
    @DubboReference
    private CommunityService communityService;
    @DubboReference
    private HouseBrokerService houseBrokerService;
    @DubboReference
    private HouseUserService houseUserService;
    @DubboReference
    private HouseImageService houseImageService;


    @RequestMapping
    public String index(HttpServletRequest request, Map map) {

        Map<String, Object> filters = getFilters(request);
        map.put("filters", filters);
        PageInfo page = houseService.findPage(filters);
        map.put("page", page);

        map.put("communityList",communityService.findAll());
        map.put("houseTypeList",dictService.findListByDictCode("houseType"));
        map.put("floorList",dictService.findListByDictCode("floor"));
        map.put("buildStructureList",dictService.findListByDictCode("buildStructure"));
        map.put("directionList",dictService.findListByDictCode("direction"));
        map.put("decorationList",dictService.findListByDictCode("decoration"));
        map.put("houseUseList",dictService.findListByDictCode("houseUse"));

     return "house/index";
    }

    @RequestMapping("/create")
    public String insert(Map map){
        map.put("communityList",communityService.findAll());
        map.put("houseTypeList",dictService.findListByDictCode("houseType"));
        map.put("floorList",dictService.findListByDictCode("floor"));
        map.put("buildStructureList",dictService.findListByDictCode("buildStructure"));
        map.put("directionList",dictService.findListByDictCode("direction"));
        map.put("decorationList",dictService.findListByDictCode("decoration"));
        map.put("houseUseList",dictService.findListByDictCode("houseUse"));
        return "house/create";
    }
    @RequestMapping("/save")
    public String save(House house){
        houseService.index(house);
        return "common/successPage";
    }
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") Long id, Map map){
        House house = houseService.getById(id);
        map.put("house",house);
        map.put("communityList",communityService.findAll());
        map.put("houseTypeList",dictService.findListByDictCode("houseType"));
        map.put("floorList",dictService.findListByDictCode("floor"));
        map.put("buildStructureList",dictService.findListByDictCode("buildStructure"));
        map.put("directionList",dictService.findListByDictCode("direction"));
        map.put("decorationList",dictService.findListByDictCode("decoration"));
        map.put("houseUseList",dictService.findListByDictCode("houseUse"));
        return "house/edit";
    }
    @RequestMapping("/update")
    public String update(House house){
        houseService.update(house);
        return "common/successPage";
    }
    @RequestMapping("/delete/{id}")
    public String delete(@PathVariable("id") Long id){
         houseService.delete(id);
        return "redirect:/house";
    }
    @RequestMapping("/publish/{id}/{status}")
    public String publish(@PathVariable("id")Long id,@PathVariable("status") int status){
        houseService.publish(id,status);
        return "redirect:/house";
    }
    @RequestMapping("/{id}")
    public String show(@PathVariable("id")Long id, ModelMap model){
        House house = houseService.getById(id);
        Community community = communityService.getById(house.getCommunityId());
        List<HouseBroker> houseBrokerList = houseBrokerService.findListByHouseId(id);
        List<HouseUser> houseUserList =houseUserService.findListByHouseId(id);
        List<HouseImage> houseImage1List = houseImageService.findList(id,1);
        List<HouseImage> houseImage2List = houseImageService.findList(id,2);
        model.addAttribute("house",house);
        model.addAttribute("house",house);
        model.addAttribute("community",community);
        model.addAttribute("houseBrokerList",houseBrokerList);
       model.addAttribute("houseUserList",houseUserList);
        model.addAttribute("houseImage1List",houseImage1List);
        model.addAttribute("houseImage2List",houseImage2List);
        return "house/show";
    }

}
